package com.briup.pts.web.controller;

import com.briup.pts.aop.Logger;
import com.briup.pts.config.response.Result;
import com.briup.pts.domain.bean.IndexRecord;
import com.briup.pts.service.IndexRecordService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@Tag(name = "指标记录管理模块")
@RestController
@RequestMapping("/indexRecord")
@RequiredArgsConstructor
public class IndexRecordController {

    private final IndexRecordService recordService;

    @Logger("保存或更新指标记录")
    @Operation(summary = "保存或更新指标记录")
    @PostMapping("/saveOrUpdate")
    public Result<?> saveOrUpdate(@RequestBody @Valid IndexRecord indexRecord){
        recordService.saveOrUpdate(indexRecord);
        return Result.success();
    }

    @Operation(summary = "分页多条件查询指标记录信息")
    @GetMapping("/query")
    public Result<?> pageQuery(
            @RequestParam(defaultValue = "1")
            @Min(value = 1, message = "当前页码不能小于1") Integer pageNum,
            @RequestParam(defaultValue = "5")
            @Min(value = 1, message = "页面不能小于1") Integer pageSize,
            Integer startWeight, Integer endWeight){

        PageInfo<IndexRecord> pageInfo = recordService
                .pageQuery(pageNum,pageSize,startWeight,endWeight);

        return Result.success(pageInfo);
    }

    @Logger("根据id删除指标记录")
    @Operation(summary = "根据id删除指标记录")
    @DeleteMapping("/deleteById/{id}")
    public Result<?> deleteById(@PathVariable Integer id){
        recordService.batchDelete(List.of(id));
        return Result.success();
    }

    @Logger("批量删除指标记录")
    @Operation(summary = "批量删除指标记录")
    @DeleteMapping("/deleteByIdAll")
    public Result<?> batchDelete(@RequestBody
         @NotEmpty(message = "待删除指标记录id必须指定") List<Integer> ids){
        recordService.batchDelete(ids);
        return Result.success();
    }
}
